{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Seattle Weather Interactive\n",
    "===========================\n",
    "This chart provides an interactive exploration of Seattle weather over the\n",
    "course of the year. It includes a one-axis brush selection to easily\n",
    "see the distribution of weather types in a particular date range.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# category: interactive\n",
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "from vega_datasets import data\n",
    "\n",
    "scale = alt.Scale(domain=['sun', 'fog', 'drizzle', 'rain', 'snow'],\n",
    "                  range=['#e7ba52', '#a7a7a7', '#aec7e8', '#1f77b4', '#9467bd'])\n",
    "color = alt.Color('weather:N', scale=scale)\n",
    "\n",
    "# We create two selections:\n",
    "# - a brush that is active on the top panel\n",
    "# - a multi-click that is active on the bottom panel\n",
    "brush = alt.selection_interval(encodings=['x'])\n",
    "click = alt.selection_multi(encodings=['color'])\n",
    "\n",
    "# Top panel is scatter plot of temperature vs time\n",
    "points = alt.Chart().mark_point().encode(\n",
    "    alt.X('date:T', timeUnit='monthdate', axis=alt.Axis(title='Date')),\n",
    "    alt.Y('temp_max:Q',\n",
    "        axis=alt.Axis(title='Maximum Daily Temperature (C)'),\n",
    "        scale=alt.Scale(domain=[-5, 40])\n",
    "    ),\n",
    "    color=alt.condition(brush, color, alt.value('lightgray')),\n",
    "    size=alt.Size('precipitation:Q', scale=alt.Scale(range=[5, 200]))\n",
    ").properties(\n",
    "    width=600,\n",
    "    height=300,\n",
    "    selection=brush\n",
    ").transform_filter(\n",
    "    click.ref()\n",
    ")\n",
    "\n",
    "# Bottom panel is a bar chart of weather type\n",
    "bars = alt.Chart().mark_bar().encode(\n",
    "    x='count()',\n",
    "    y='weather:N',\n",
    "    color=alt.condition(click, color, alt.value('lightgray')),\n",
    ").transform_filter(\n",
    "    brush.ref()\n",
    ").properties(\n",
    "    width=600,\n",
    "    selection=click\n",
    ")\n",
    "\n",
    "alt.vconcat(points, bars,\n",
    "    data=data.seattle_weather.url,\n",
    "    title=\"Seattle Weather: 2012-2015\"\n",
    ")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
